package se.webbzon.oschi01.utils;

import java.awt.Component;
import java.awt.Graphics;
import java.awt.image.BufferedImage;

import javax.swing.Icon;

public class Icons {
	
	public static final int ICON_STILL = 1 << 0;
	
	public static final Icon resize(final Icon icon, final int width, final int height) {
		return resize(icon,width,height,ICON_STILL);
	}
	
	public static final Icon resize(final Icon icon, final int width, final int height, final int properties) {
		final BufferedImage image = new BufferedImage(icon.getIconWidth(),icon.getIconHeight(),BufferedImage.TYPE_INT_ARGB);
		if (Binary.isSet(properties, ICON_STILL))
			icon.paintIcon(null, image.getGraphics(), 0, 0);
		
		return new Icon() {

			@Override public int getIconHeight() {
				return width;
			}

			@Override public int getIconWidth() {
				return height;
			}

			@Override public void paintIcon(Component c, Graphics g, int x, int y) {
				if (!Binary.isSet(properties, ICON_STILL))
					icon.paintIcon(null, image.getGraphics(), 0, 0);
				g.drawImage(image, x, y, width, height, c);
			}
			
		};
	}

}
